package com.google.android.daca;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.daca.service.DoodleRESTService;
import com.google.android.daca.types.CreatePollResponse;
import com.google.android.daca.types.Initiator;
import com.google.android.daca.types.Option;
import com.google.android.daca.types.Poll;

public class OptionsActivity extends ListActivity implements OnClickListener,
		OnKeyListener {

	private static final String CLASS_TAG = OptionsActivity.class
			.getSimpleName();
	private static final String TAG_TEMP = "TAG_TEMP";
	private LinearLayout containerView = null;
	private Button addOptions;
	private Button nextBtn;
	private Button finishBtn;
	private EditText mUserText;

	private ArrayAdapter<String> mAdapter;

	private ArrayList<String> mStrings = new ArrayList<String>();
	private Button addBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_12);
		Log.d(TAG_TEMP, CLASS_TAG);
		mAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, mStrings);

		setListAdapter(mAdapter);

		mUserText = (EditText) findViewById(R.id.userText);

		mUserText.setOnClickListener(this);
		mUserText.setOnKeyListener(this);
		
		addBtn = (Button) findViewById(R.id.add);
		addBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				sendText();
			}
		});

		nextBtn = (Button) findViewById(R.id.next_1);
		nextBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String initatorName = null;
				String pollTittle = null;
				String pollDescription = null;
				DoodleRESTService service = new DoodleRESTService();
				Bundle bundle = getIntent().getExtras();

				if (bundle != null) {
					initatorName = bundle.getString("Initiator");
					pollTittle = bundle.getString("Title");
					pollDescription = bundle.getString("Description");
				}
				Poll poll = new Poll();
				poll.setTitle(pollTittle);
				Initiator initiator = new Initiator();
				initiator.setName(initatorName);
				poll.setInitiator(initiator);
				poll.setDescription(pollDescription);
				List<Option> options = new ArrayList<Option>();
				
				if(mAdapter.getCount() == 0) {
					Toast.makeText(getApplicationContext(),
							"You need to enter at least one option", Toast.LENGTH_LONG).show();
					return;
				}
				
				for (int i = 0; i < mAdapter.getCount(); i++) {
					Option option = new Option();
					option.setValue(mAdapter.getItem(i));
					options.add(option);
				}
				poll.setOptions(options);
				try {
					CreatePollResponse response = service.createPoll(poll);
					showPollID(response.getId(), response.getAdminKey());

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});

	}

	private void showPollID(String pollID, String adminKey) {
		final FrameLayout frame = new FrameLayout(this);
		LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		TableLayout mTable = (TableLayout) inflater.inflate(R.layout.msg_table,
				null);
		TextView cell12 = (TextView) mTable.findViewById(R.id.poll_id);
		TextView cell22 = (TextView) mTable.findViewById(R.id.admin_key);
		cell12.setText(pollID);
		cell22.setText(adminKey);
		frame.addView(mTable, new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.FILL_PARENT,
				FrameLayout.LayoutParams.WRAP_CONTENT));
		
		new AlertDialog.Builder(this)
			.setView(frame)
			.setTitle(getResources().getString(R.string.poll_info))
			.setPositiveButton("Done", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						// in this case, we redirect to Create Poll Activity
						finish();
					}
				}).show();
	}

	public void onClick(View v) {
		sendText();
	}

	private void sendText() {
		String text = mUserText.getText().toString();
		mAdapter.add(text);
		mUserText.setText(null);
	}

	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			switch (keyCode) {
			case KeyEvent.KEYCODE_DPAD_CENTER:
			case KeyEvent.KEYCODE_ENTER:
				sendText();
				return true;
			}
		}
		return false;
	}

}
